﻿using K9Nano.IM.Models;
using K9Nano.LayIM;
using K9Nano.LayIM.Models;
using Microsoft.Extensions.Options;
using WalkingTec.Mvvm.Core;

namespace K9Nano.IM.LayIM
{
    public class ChatStoreBase
    {
        protected readonly IDataContext Db;
        protected readonly LayIMOptions Options;

        public ChatStoreBase(IDataContext db, IOptions<LayIMOptions> options)
        {
            Db = db;
            Options = options.Value;
        }

        protected virtual LayIMUser MapToImUser(User user)
        {
            var im = new LayIMUser
            {
                id = user.ID.ToString(),
                sign = user.Sign,
                status = user.Online ? "online" : "hide",
                username = user.Nickname ?? user.Name,
                avatar = GetAvatar(user)
            };

            return im;
        }

        protected virtual string GetAvatar(User user)
        {
            if (!string.IsNullOrEmpty(user.Avatar))
            {
                return user.Avatar;
            }

            if (user.PhotoId != null)
            {
                return "/api/_file/GetFile/" + user.PhotoId;
            }
            return Options.DefaultUserAvatar;
        }
    }
}